﻿function loadPic(picURL,mcTarget)
{
	//trace("loadPic:" + mcTarget);
	var mclListener:Object = new Object();
	var imageLoader_mc:MovieClipLoader = new MovieClipLoader();
	mclListener.onLoadError = function(mcTarget:MovieClip,errorCode:String, httpStatus:Number)
	{
		name_txt = mcTarget._parent.createTextField("name_txt", -1, 0, 0, 50, 50);
		name_txt.text = mcTarget + "," + errorCode + "," + httpStatus;
	}
	mclListener.onLoadInit = function(mcTarget:MovieClip):Void {
		mcTarget.loadComplete = false;
		slowShowMC(mcTarget);
	}
	imageLoader_mc.addListener(mclListener);
	imageLoader_mc.loadClip(picURL,mcTarget);
	//showMCBlock(mcTarget._parent, 0x000000, 10, part_mc.cname);
	//trace(mcTarget._parent._parent.loading_mc);
}
function loadPicNoSlowShow(picURL,mcTarget)
{
	//trace("loadPic:" + mcTarget);
	var mclListener:Object = new Object();
	var imageLoader_mc:MovieClipLoader = new MovieClipLoader();
	mclListener.onLoadError = function(mcTarget:MovieClip,errorCode:String, httpStatus:Number)
	{
		//name_txt = mcTarget._parent.createTextField("name_txt", -1, 0, 0, 50, 50);
		//name_txt.text = mcTarget + "," + errorCode + "," + httpStatus;
	}
	mclListener.onLoadInit = function(mcTarget:MovieClip):Void {
		mcTarget.loadComplete = true;
	}
	imageLoader_mc.addListener(mclListener);
	imageLoader_mc.loadClip(picURL,mcTarget);
	//showMCBlock(mcTarget._parent, 0x000000, 10, part_mc.cname);
	//trace(mcTarget._parent._parent.loading_mc);
}

function slowShowMC(target)
{
	//trace("slowShowMC:" + target);
	target._alpha = 0;
	target.onEnterFrame = function()
	{
		if (this._alpha<100)
		{
			this._alpha = this._alpha + 10;
		}
		else
		{
			this._alpha = 100;
			this.onEnterFrame = null;
			target.loadComplete = true;
		}
	}
}
function slowHideMC(target){
	target.onEnterFrame = function()
	{
		if (this._alpha>0)
		{
			this._alpha = this._alpha - 10;
		}
		else
		{
			this._alpha = 0;
			this.onEnterFrame = null;
			//trace(this.optionValue_mc);
			if (this.optionValue_mc!=undefined){
				if (this.posX=="relative") this._x = -10000;
				if (this.posY=="relative") this._y = -10000;
			}
			
		}
	}
}

function showMsg(target_mc:MovieClip, msg:String, posX:Number, posY:Number, mWidth:Number, mHeight:Number, fontColor:Number){
	tipMsg = target_mc.msg_mc;
	
	if (tipMsg == undefined){
		tipMsg = target_mc.createTextField("msg_mc", -1, posX, posY, mWidth, mHeight);
		tipMsg.multiline = true;
		tipMsg.wordWrap = true;
	}
	
	tipMsg._x = posX;
	tipMsg._y = posY;
	tipMsg.text = msg;
	
	var tipMsg_fmt:TextFormat = new TextFormat();
	tipMsg_fmt.color = fontColor;
	tipMsg_fmt.font = "Arial";
	tipMsg_fmt.size = 15;
	tipMsg.setTextFormat(tipMsg_fmt);
	//trace(msg);
}

function showMCBlock(_mc, bFillColor, bFillAlpha, tipMsg){
	rectangle_mc = _mc.createEmptyMovieClip("rectangle_mc",-2);
	drawRoundedRectangle(rectangle_mc, 40, 40, 10, bFillColor, bFillAlpha, false, false);
	showMsg(_mc, tipMsg, 0, 0, 40, 40, 0x000000);
}

//draw RoundedRectangle
import flash.filters.BevelFilter;
import flash.filters.GlowFilter;
function drawRoundedRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, cornerRadius:Number, fillColor:Number, fillAlpha:Number, ifBevel:Boolean, ifGlow:Boolean):Void {
    rectangle_mc = target_mc;
	with (rectangle_mc) {
        beginFill(fillColor, fillAlpha);
        moveTo(cornerRadius, 0);
        lineTo(boxWidth - cornerRadius, 0);
        curveTo(boxWidth, 0, boxWidth, cornerRadius);
        lineTo(boxWidth, cornerRadius);
        lineTo(boxWidth, boxHeight - cornerRadius);
        curveTo(boxWidth, boxHeight, boxWidth - cornerRadius, boxHeight);
        lineTo(boxWidth - cornerRadius, boxHeight);
        lineTo(cornerRadius, boxHeight);
        curveTo(0, boxHeight, 0, boxHeight - cornerRadius);
        lineTo(0, boxHeight - cornerRadius);
        lineTo(0, cornerRadius);
        curveTo(0, 0, cornerRadius, 0);
        lineTo(cornerRadius, 0);
        endFill();
    }
	var filterArray:Array = new Array();
	if (ifBevel)//set BevelFilter
	{
		var filter:BevelFilter = new BevelFilter(2, 45, 0xFFFFFF, 0.4, 0x000000, 0.2, 6, 6, 1, 3, "inner", false);
		filterArray.push(filter);
	}
	if (ifGlow)//set glow
	{
		var filter:GlowFilter = new GlowFilter(0xFFFFFF, 0.4, 2, 2, 1, 3, false,false); 
		filterArray.push(filter);
	}
	rectangle_mc.filters = filterArray;	
	
	//trace(rectangle_mc + "," + rectangle_mc._width + "," + rectangle_mc._height+ "," + target_mc._x+ "," + target_mc._y);
}

//Tip Box
function showTip(str,obj,posX,posY)
{
	if (tipBox_mc == undefined)
	{
		tipBox_mc = this.attachMovie("tipBox","tipBox_mc",this.getNextHighestDepth());
	}
	tipBox_mc.setTip(str);
	tipBox_mc._txt.text = str;
	tipBox_mc._txt._width = tipBox_mc._txt.textWidth + 10;
	tipBox_mc.border_mc.borderCenter._x = tipBox_mc.border_mc.borderLeft._x + tipBox_mc.border_mc.borderLeft._width;
	tipBox_mc.border_mc.borderCenter._width = tipBox_mc._txt._width;
	tipBox_mc.border_mc.borderRight._x = tipBox_mc.border_mc.borderCenter._x + tipBox_mc.border_mc.borderCenter._width;
	tipBox_mc.border_mc.arrow_mc._x = Math.floor((tipBox_mc.border_mc.borderRight._x + tipBox_mc.border_mc.borderRight._width - tipBox_mc.border_mc.arrow_mc._width)/2);
	
	this.CreateDropShadow(tipBox_mc.border_mc);
	
	tipBox_mc._x = posX - Math.floor((tipBox_mc._width - obj._width)/2);
	tipBox_mc._y = posY;
	//auto hidden
	//tipBox_mc.showTime = 30;
	//i=0;
	//tipBox_mc.onEnterFrame = function()
	//{
		//if (i<this.showTime) 
			//i++;
		//else
			//hiddenTip();
	//}
}
function hiddenTip()
{
	tipBox_mc._x = -10000;
}
//reture object's x and y value in stage
function stageX(obj)
{
	var x = 0;
	while (obj!=_root)
	{
		x += obj._x;
		obj = obj._parent;
	}
	return x;
}
function stageY(obj)
{
	var y = 0;
	while (obj!=_root)
	{
		y += obj._y;
		obj = obj._parent;
	}
	return y;
}

function newButton(parent_mc, Button_mc, currentNum, elementRepeat, elementWidth, elementHeight, elementGap, elementPerScreen){
	//trace(Button_mc + "\n");
	if (currentNum > 0){
		if (elementRepeat == "x")
			Button_mc._x = currentNum * (elementWidth + elementGap);
		else if (elementRepeat == "y")
			Button_mc._y = currentNum * (elementHeight + elementGap);
		//trace(currentNum);
		if (currentNum >= elementPerScreen){
			createBtnScroller(parent_mc);
		}
	}
	if (Button_mc.imgUrl!= undefined){
		pic_mc = Button_mc.createEmptyMovieClip("pic_mc", -1);
		loadPic(Button_mc.imgUrl, pic_mc);
	}
	else{
		text_mc = Button_mc.createTextField("text_mc", -1, 0, 0, elementWidth, elementHeight);
		text_mc.text = Button_mc.displayName;
	}
	bg_mc = Button_mc.createEmptyMovieClip("bg_mc",-2);
	drawRoundedRectangle(bg_mc, elementWidth, elementHeight, 10, 0xffffff, 30, false, false);
	bg_mc._alpha = 0;

	Button_mc.onRollOver = function(){
		showTip(this.displayName, this, stageX(this) , stageY(this) - 20);
	}
	Button_mc.onRollOut = function(){
		hiddenTip();
	}
}
function createBtnScroller(content_mc){
	var parent_mc = content_mc._parent;
	content_mc.scrollerIsShow = true;
	mask_mc = parent_mc.createEmptyMovieClip("mask_mc", parent_mc.getNextHighestDepth());
	mask_mc._x = content_mc._x;
	mask_mc._y = content_mc._y;
	if (content_mc.elementRepeat == "x"){
		mask_mc.width = (content_mc.elementWidth + content_mc.elementGap) * content_mc.elementPerScreen - content_mc.elementGap;
		mask_mc.height = content_mc.elementHeight;
	}
	else if (content_mc.elementRepeat == "y"){
		mask_mc.height = (content_mc.elementHeight + content_mc.elementGap) * content_mc.elementPerScreen - content_mc.elementGap;
		mask_mc.width = content_mc.elementWidth;
	}
	drawRoundedRectangle(mask_mc, mask_mc.width, mask_mc.height, 0, 0x000000, 0, false, false);
	content_mc.setMask(mask_mc);
	scroller_mc = parent_mc.createEmptyMovieClip("scroller_mc", parent_mc.getNextHighestDepth());
	scrollLeft_mc = scroller_mc.createEmptyMovieClip("scrollLeft_mc", scroller_mc.getNextHighestDepth());
	loadPicNoSlowShow(content_mc.arrowImg, scrollLeft_mc);
	scrollRight_mc = scroller_mc.createEmptyMovieClip("scrollRight_mc", scroller_mc.getNextHighestDepth());
	loadPicNoSlowShow(content_mc.arrowImg, scrollRight_mc);
	scrollRight_mc._xscale = -100;
	parent_mc.onEnterFrame = function()
	{
		var scrollRight_mc = this.scroller_mc.scrollRight_mc;
		var scrollLeft_mc = this.scroller_mc.scrollLeft_mc;
		var mask_mc = this.mask_mc;
		if (scrollRight_mc.loadComplete && scrollLeft_mc.loadComplete){
			this.onEnterFrame = null;
			scrollLeft_mc._alpha=0; scrollRight_mc._alpha=0;
			if (content_mc.elementRepeat == "x"){
				scrollLeft_mc._x = mask_mc._x - scrollLeft_mc._width - 5;
				scrollRight_mc._x = mask_mc._x + mask_mc._width + scrollRight_mc._width + 5;
				scrollLeft_mc._y = mask_mc._y;
				scrollRight_mc._y = mask_mc._y;
			}
			else if (content_mc.elementRepeat == "y"){
				scrollLeft_mc._y = mask_mc._y - scrollLeft_mc._height - 5;
				scrollRight_mc._y = mask_mc._y + mask_mc._height + scrollRight_mc._height + 5;
				scrollLeft_mc._x = mask_mc._x;
				scrollRight_mc._x = mask_mc._x;
			}
			setScrollAction(content_mc, mask_mc, scrollLeft_mc, scrollRight_mc);
			resetScrollerBtn(content_mc, mask_mc, scrollLeft_mc, scrollRight_mc);
		}
	}
	
}
function setScrollAction(content_mc, mask_mc, scrollLeft_mc, scrollRight_mc){
	var isScrolling = false;
	scrollLeft_mc.onRelease = function(){
		if (content_mc.elementRepeat == "x" && content_mc._x < mask_mc._x && !isScrolling){
			isScrolling = true;
			targetX = content_mc._x + content_mc.elementWidth + content_mc.elementGap;
			this.onEnterFrame= function(){
				content_mc._x += content_mc.scrollSpeed;
				if (content_mc._x >= targetX){
					this.onEnterFrame = null;
					content_mc._x = targetX;
					isScrolling = false;
					resetScrollerBtn(content_mc, mask_mc, scrollLeft_mc, scrollRight_mc);
				}
			}
		}
		if (content_mc.elementRepeat == "y" && content_mc._y < mask_mc._y && !isScrolling){
			isScrolling = true;
			targetY = content_mc._y + content_mc.elementHeight + content_mc.elementGap;
			this.onEnterFrame= function(){
				content_mc._y += content_mc.scrollSpeed;
				if (content_mc._y >= targetY){
					this.onEnterFrame = null;
					content_mc._y = targetY;
					isScrolling = false;
					resetScrollerBtn(content_mc, mask_mc, scrollLeft_mc, scrollRight_mc);
				}
			}
		}
	}
	scrollRight_mc.onRelease = function(){
		if (content_mc.elementRepeat == "x" && (content_mc._x + content_mc._width) > (mask_mc._x + mask_mc._width) && !isScrolling){
			isScrolling = true;
			targetX = content_mc._x - content_mc.elementWidth - content_mc.elementGap;
			this.onEnterFrame= function(){
				content_mc._x -= content_mc.scrollSpeed;
				if (content_mc._x <= targetX){
					this.onEnterFrame = null;
					content_mc._x = targetX;
					isScrolling = false;
					resetScrollerBtn(content_mc, mask_mc, scrollLeft_mc, scrollRight_mc);
				}
			}
		}
		if (content_mc.elementRepeat == "y" && (content_mc._y + content_mc._height) > (mask_mc._y + mask_mc._height) && !isScrolling){
			isScrolling = true;
			targetY = content_mc._y - content_mc.elementHeight - content_mc.elementGap;
			this.onEnterFrame= function(){
				content_mc._y -= content_mc.scrollSpeed;
				if (content_mc._y <= targetY){
					this.onEnterFrame = null;
					content_mc._y = targetY;
					isScrolling = false;
					resetScrollerBtn(content_mc, mask_mc, scrollLeft_mc, scrollRight_mc);
				}
			}
		}
	}
}
function resetScrollerBtn(content_mc, mask_mc, scrollLeft_mc, scrollRight_mc){
	scrollLeft_mc.enabled = false;
	scrollLeft_mc._alpha = 30;
	scrollRight_mc.enabled = false;
	scrollRight_mc._alpha = 30;
	if ((content_mc.elementRepeat == "x" && content_mc._x < mask_mc._x) || (content_mc.elementRepeat == "y" && content_mc._y < mask_mc._y))
	{
		scrollLeft_mc.enabled = true;
		scrollLeft_mc._alpha = 100;
	}

	if ((content_mc.elementRepeat == "x" && (content_mc._x + content_mc._width) > (mask_mc._x + mask_mc._width)) || (content_mc.elementRepeat == "y" && (content_mc._y + content_mc._height) > (mask_mc._y + mask_mc._height)))
	{
		scrollRight_mc.enabled = true;
		scrollRight_mc._alpha = 100;
	}
}
function addApply(_mc){
	appliedPart += _mc.id + ",";
	//trace(appliedPart + "\n");
}
function ifApply(id){
	if (appliedPart.indexOf(id)>=0)
		return true;
	else
		return false;
}
function removeApply(id){
	appliedPart = appliedPart.split(id + ",").join("");
	//trace(appliedPart + "\n");
}